--[[
    NuckPluck - An android game for learning the Thai alphabet
    Copyright (C) 2013 Nathan Kieffer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Contact: nathankieffer@gmail.com
--]]

local physics = require("physics")
local vector = require("vector")
local Enemy = {}

function pulsate(bubble)
   --recursive enclosure function used to make the bubbles move continuosly at random
   return function(event)
      transition.to(bubble, {xScale=0.5, yScale=0.5, x=math.random(-20,20), y=math.random(-20,20),time=500,
			  onComplete=function(event)
			     transition.to(bubble, {xScale=1, yScale=1, 
						    x=math.random(-20,20), y=math.random(-20,20),
						    time=500,
						    onComplete=pulsate(bubble)})
			    end})
   end
end
    

function Enemy.newEnemy(isHead, speed)
   local enemy = display.newGroup()
   enemy.type="enemy"
   enemy.name="enemy"
   enemy.isHead = isHead
   enemy.speed = math.random(1,speed) 
   enemy.trackRate = 200
   enemy.rotDir = 1
   enemy.numBubbles = math.random(30)+5
   enemy.maxBubbles = enemy.numBubbles+10
   enemy.bubbleRange = 10

   enemy.waiting = 0
   function enemy:setStartPosition(start)
      if start then
	 self.x = start.x * 50
	 self.y = start.y * 50
      else
	 local side = math.random(2)
	 local tb = {150, self.parent.contentWidth - 150}
	 local lr = {150, self.parent.contentHeight - 150}
	 local x1 = math.random(150, self.parent.contentWidth - 150 )
	 local x2 = tb[math.random(2)]
	 local y1 = math.random(150, self.parent.contentHeight - 150)
	 local y2 = lr[math.random(2)]
	 
	 if side == 1 then
	    self.x = x1
	    self.y = y2
	 else
	    self.x = x2
	    self.y = y1
	 end
	 --      self.x=0
	 --     self.y=0
      end
   end

   function enemy:track(player)
      --every frame, this function is called and recalculates the 
      --enemies position based on the position of the player
      local vect = vector.fromPoints({x=self.x,y=self.y},
				     {x=player.x, y=player.y})
      local mag = vect:getMagnitude()
      vect:normalize()
      if mag > 25 then
	 self.oldx = self.x
	 self.oldy = self.y
	 self.x = self.x + (vect.x * self.speed)
	 self.y = self.y + (vect.y * self.speed)
      end
      
   end

   function enemy:addBubble()
      local size = math.random(5,20)
      local bubble = display.newCircle(self, math.random(10), math.random(10), size)
     -- bubble:setFillColor(0,0,0,0)
      bubble:setStrokeColor(math.random(0,0.4),
			    math.random(),
			    math.random(),
			    math.random()
)
      bubble.strokeWidth=2
			    
      bubble:setFillColor(math.random() - 0.6,
			  math.random(),
			  math.random() - 0.6,
			  math.random()
      )
      bubble.pulsate = size
      bubble.pulsdir = -1
      bubble.maxSize = size
      bubble.minSize = 10
      pulsate(bubble)(nil)
   end

   function enemy:enterFrame(event)
      --add, move, resize and rotate elements of enemy graphic
      if math.random(100) > 95 and self.numBubbles < self.maxBubbles then
	 self:addBubble()
	 self.numBubbles = self.numBubbles + 1
	 self.rotDir = self.rotDir * -1
      end
      self:rotate(math.random(5) * self.rotDir)
      if math.random(100) == 100 then
	 self.bubbleRange = self.bubbleRange + math.random(5)
      end
      if self.isHead then
	 self.eye1:toFront()
	 self.eye2:toFront()
	 self.eye1:rotate(5)
	 self.eye2:rotate(-5)
      end
      self:toFront()
   end

   for i=5, math.random(30)+5 do
      enemy:addBubble()
   end
   if enemy.isHead then
      enemy.eye1 = display.newGroup()
      enemy.eye2 = display.newGroup()
      local ret1 = display.newCircle(enemy.eye1, 0,0, math.random(3,7))
      local ret2 = display.newCircle(enemy.eye2, 0,0, 5)
      local pup1 = display.newCircle(enemy.eye1, 2,2, 2)
      pup1:setFillColor(0)
      local pup2 = display.newCircle(enemy.eye2, -2,-2, math.random(1,4))
      pup2:setFillColor(0)
      enemy:insert(enemy.eye1)
      enemy.eye1.x = 0
      enemy.eye1.y = 0
      enemy:insert(enemy.eye2)
      enemy.eye2.x = 15
      enemy.eye2.y = 0
   end

   physics.addBody(enemy, "dynamic", { filter={categoryBits=8,
					       maskBits=29}})
   Runtime:addEventListener("enterFrame", enemy)
   return enemy
end

return Enemy